package com.example.unruly_zhao.mopaide.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;

import com.example.unruly_zhao.mopaide.R;
import com.example.unruly_zhao.mopaide.lock_screen.LockscreenUtil;
import com.example.unruly_zhao.mopaide.lock_screen.PermissionActivity;
import com.umeng.analytics.MobclickAgent;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Unruly_Zhao on 2017/8/18.
 * 热点指数+
 */

public class Activity_Hotspot extends Activity {

    private WindowManager mWindowManager;
    private View mLockscreenView2 = null;
    private WindowManager.LayoutParams mParams;
    private LayoutInflater mInflater = null;
    private boolean mIsLockEnable = false;
    private boolean mIsSoftkeyEnable = false;
    private Button mBt_hot_finish;  // 点击跳回主界面
    private ImageView mIm_hot_back;
    private ImageView mIm_hot_next;
    private WebView mWb_hot;
    private Animation mRotate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initState();
        initView();
        attachLockScreenView();

    }

    private void attachLockScreenView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent permissionActivityIntent = new Intent(this, PermissionActivity.class);

                permissionActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(permissionActivityIntent);

                LockscreenUtil.getInstance(this).getPermissionCheckSubject()
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                new Action1<Boolean>() {
                                    @Override
                                    public void call(Boolean aBoolean) {

                                        addLockScreenView();

                                    }
                                }
                        );
            } else {
                addLockScreenView();
            }
        } else {
            addLockScreenView();
        }

    }

    private void addLockScreenView() {
        if (null != mWindowManager && null != mLockscreenView2 && null != mParams) {

            mLockscreenView2.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    return false;

                }
            });
            mWindowManager.addView(mLockscreenView2, mParams);
            settingLockView();
        }
    }

    private void settingLockView() {


        mBt_hot_finish = (Button) mLockscreenView2.findViewById(R.id.title_finish);  //点击调回主界面

        // mRotate = AnimationUtils.loadAnimation(Activity_Hotspot.this, R.anim.refresh_anim); // 创建动画

        mIm_hot_back = (ImageView) mLockscreenView2.findViewById(R.id.title_back);  //返回键  webview返回上一界面

        into_back();

        mIm_hot_next = (ImageView) mLockscreenView2.findViewById(R.id.title_next); //快进键 webview前进上一界

        init_next();

        mWb_hot = (WebView) mLockscreenView2.findViewById(R.id.hotpost_wb); //加载热点网页

        mWb_hot.loadUrl("https://top.baidu.com/");

        init_mWb_hot();

        //mWb_hot.getUrl();  //获取当前webview地址


    }

    // 点击button返回上一界面

    private void init_mWb_hot() {

        mBt_hot_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Activity_Hotspot.this, Activity_HomePage.class);

                startActivity(intent);

                finish();

            }
        });

    }

    // 前进键
    private void init_next() {

        mIm_hot_next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mWb_hot.goForward();

            }
        });

    }

    // 关闭当前界面返回上一界面

    private void into_back() {

        mIm_hot_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mWb_hot.goBack();

            }
        });


    }


    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }


    /**
     * 初始化视图
     */
    private void initView() {
        if (null == mInflater) { //如果布局填空器为空 ，创建系统自带的
            mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        if (null == mLockscreenView2) {  //如果视图为空创建视图管理找到视图

            mLockscreenView2 = mInflater.inflate(R.layout.activity_hotspot, null);  // 原版本的

        }
    }

    private void initState() {

        mIsLockEnable = LockscreenUtil.getInstance(this).isStandardKeyguardState();
        if (mIsLockEnable) {
            //WindowManager.LayoutParams 是 WindowManager 接口的嵌套类；继承于 ViewGroup.LayoutParams
            mParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,//系统内部错误提示，显示于所有内容之上。
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // 不许获得焦点。
                    PixelFormat.TRANSLUCENT);  //设置为透明
        } else {
            mParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,

                    PixelFormat.TRANSLUCENT);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (mIsLockEnable && mIsSoftkeyEnable) {
                mParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; //半透明
            } else {
                mParams.flags = WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; // 背景图片
            }
        } else {
            mParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        }

        if (null == mWindowManager) {
            mWindowManager = ((WindowManager) this.getSystemService(WINDOW_SERVICE)); // 获取系统服务

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
